<template>
  <div class="addressList">
    <!------------------------------------------------------返回导航------------------------------------------------------------------>
    <HeaderTitle :title="WebName"></HeaderTitle>
    <van-pull-refresh v-model="MianLoading" @refresh="onRefresh">
      <!------------------------------------------------------评价------------------------------------------------------------------>
      <van-address-list
        v-model="chosenAddressId"
        :list="newList"
        @add="onAdd"
        @edit="onEdit"
      />
    </van-pull-refresh>
  </div>
</template>
<script>
/* ... */
import HeaderTitle from '@/components/HeaderTitle.vue'
import { getMyAddressById } from '@/api/api'

export default {
  name: 'addresslist',

  mixins: [],

  components: {
    HeaderTitle
  },

  props: {},

  data () {
    return {
      WebName: '管理收货地址',
      MianLoading: false,
      count: 0,
      chosenAddressId: 0,
      list: [ ],
      newList: []
    }
  },

  computed: {

  },

  watch: {},

  created () {
    if (localStorage.getItem('X-Auth-Token')) {
      this.PostData()
    } else {
      this.$toast.fail('您未曾登陆')
      this.$router.push('./login?tabs=0')
    }
  },

  mounted () {},

  destroyed () {},

  methods: {
    NewList () {
      for (let i = 0; i < this.list.length; i++) {
        var data = {
          'address': this.list[i].province + this.list[i].county + this.list[i].city + this.list[i].address,
          'name': this.list[i].name,
          'tel': this.list[i].phone,
          'id': this.list[i].id
        }
        this.newList.push(data)
      }
    },
    PostData () {
      let postData = { }
      getMyAddressById(this.$qs.stringify(postData))
        .then(res => {
          // console.log(res.data.data.user[0])
          this.list = res.data.data
          this.NewList()
          // console.log(this.newList)
          if (res.data.code === 200) {

          } else if (res.data.code === 500) {
            this.$toast(res.data.message)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 下拉刷新事件
    onRefresh () {
      setTimeout(() => {
        // this.$toast('刷新成功')
        this.MianLoading = false
        this.count++
        this.list = []
        this.newList = []
        this.PostData()
      }, 500)
    },
    onAdd () {
      this.$router.push('/editaddress')
    },

    onEdit (item, index) {
      // this.$toast('编辑地址:' + index + item)
      console.log(item)
    }
  }
}
</script>

<style lang="less" >
/* ... */

.container {
  margin: 0 15px;
}
.addressList {
  height: 100%;
  background-color: #f6f6f6;
  .van-pull-refresh {
    height: calc(100% - 50px - 45px);
    overflow-y: auto;
    .van-pull-refresh__track {
      margin-top: 10px;
      .van-address-list{
        .van-radio-group{
          .van-cell{
            .van-cell__value{
              .van-radio{
                .van-radio__icon{
                  .van-icon{
                  }
                }
              }
            }
          }
        }
        .van-button {
          background-color: #d7b782;
          border: 0.02667rem solid #d7b782;
        }
      }
    }
  }
}
</style>
